package com.ywgt;

public class DoubleLinkedList {
    public static void main(String[] args) {
        DoubleList doubleList=new DoubleList();
        DoubleNode nodeHead = doubleList.getNodeHead();
        doubleList.add(new DoubleNode(1));
        doubleList.add(new DoubleNode(2));
        doubleList.add(new DoubleNode(3));
        doubleList.add(new DoubleNode(4));
        doubleList.print(nodeHead);
        doubleList.delete(nodeHead,4);
        System.out.println("------------------>");
        doubleList.print(nodeHead);
    }
}
class DoubleList{
    private DoubleNode nodeHead=new DoubleNode(0);

    public DoubleNode getNodeHead() {
        return nodeHead;
    }

    public void add(DoubleNode doubleNode){

        DoubleNode tem=nodeHead;
        while(tem!=null){
            if(tem.next==null){
                tem.next=doubleNode;
                doubleNode.pre=tem;
                return;
            }
            tem=tem.next;
        }
    }

    public void print(DoubleNode nodeHead){
        if(nodeHead==null){
            System.out.println("链表为空");
        }
        DoubleNode tem=nodeHead.next;
        while (tem!=null){
            System.out.println(tem);
            tem=tem.next;
        }
    }

    public void delete(DoubleNode node,int no){
        if(node==null){
            System.out.println("空链表");
            return;
        }
        DoubleNode temp=node.next;
        while(temp!=null){
            if(temp.no==no){
                temp.next.pre=temp.pre;
                if(temp.next!=null) {
                    temp.pre.next = temp.next;
                }
                return;
            }
            temp=temp.next;
        }
    }
}
class DoubleNode{
    DoubleNode pre;
    DoubleNode next;
    int no;
    public DoubleNode(int no){
        this.no=no;
    }

    @Override
    public String toString() {
        return "DoubleNode{" +
                "no=" + no +
                '}';
    }
}
